var http = require('http');
var path = require('path');
var querystring = require('querystring');

var srv = http.createServer(function (req, res) {
    // POST
    if (req.method == 'POST') {
        if (req.url == '/') {
            var data = "";
            req.on('data', function (chunk) {
                data = data + chunk;
            })
            req.on('end', function () {
                data = querystring.parse(data);
                dir_data = readLocalFile(data.up_url);
                if (dir_data != false) {
                    res.setHeader('Access-Control-Allow-Origin', '*');
                    res.writeHead(200, { 'Content-Type': 'application/json' });
                    res.end(JSON.stringify({
                        'error_code': 0, 'error_msg': 'OK',
                        'dir_data': dir_data, 'dir_path': data.up_url
                    }));
                } else {
                    res.setHeader('Access-Control-Allow-Origin', '*');
                    res.writeHead(200, { 'Content-Type': 'application/json' });
                    res.end(JSON.stringify({ 'error_code': 400, 'error_msg': 'Cannot read url!' }))
                }
            })
        } else if (req.url == '/back') {
            var data = "";
            req.on('data', function (chunk) {
                data = data + chunk;
            });
            req.on('data', function () {
                data = querystring.parse(data);
                dir_data = readLocalFile(path.resolve(data['up_url'], '..'));
                if (dir_data != false) {
                    res.setHeader('Access-Control-Allow-Origin', '*');
                    res.writeHead(200, { 'Content-Type': 'application/json' });
                    console.log(dir_data);
                    res.end(JSON.stringify({
                        'error_code': 0, 'error_msg': 'OK',
                        'dir_data': dir_data, 'dir_path': path.resolve(data['up_url'], '..')
                    }));
                } else {
                    res.setHeader('Access-Control-Allow-Origin', '*');
                    res.writeHead(200, { 'Content-Type': 'application/json' });
                    res.end(JSON.stringify({ 'error_code': 400, 'error_msg': 'Cannot read url!' }))
                }
            });
        } else if (req.url == '/doubleClick') {
            var data = "";
            req.on('data', function (chunk) {
                data = data + chunk;
            })
            req.on('end', function () {
                data = querystring.parse(data);
                console.log(data);
                if (data.isDirectory == "true") {
                    dir_data = readLocalFile(path.join(data.up_url, data.name));
                    if (dir_data != false) {
                        res.setHeader('Access-Control-Allow-Origin', '*');
                        res.writeHead(200, { 'Content-Type': 'application/json' });
                        res.end(JSON.stringify({
                            'error_code': 0, 'error_msg': 'OK',
                            'dir_data': dir_data, 'dir_path': path.join(data.up_url, data.name)
                        }));
                    } else {
                        res.setHeader('Access-Control-Allow-Origin', '*');
                        res.writeHead(200, { 'Content-Type': 'application/json' });
                        res.end(JSON.stringify({ 'error_code': 400, 'error_msg': 'Cannot read url!' }))
                    }
                } else if (data.isDirectory == "false") {
                    res.setHeader('Access-Control-Allow-Origin', '*');
                    res.writeHead(200, { 'Content-Type': 'application/json' });
                    res.end(JSON.stringify({
                        'error_code': 100, 'error_msg': 'OK',
                        'file_path': path.join(data.up_url, data.name.replace('.gif', '.aep'))
                    }));
                }
            });
        }
    } else if (req.method == 'GET') {
        // console.log(req.url);
        if (req.url = '/') {
            new_path = 'C:\\Users\\XJJY\\Desktop\\project\\smoke';
            dir_data = readLocalFile(new_path);
            res.setHeader('Access-Control-Allow-Origin', '*');
            res.writeHead(200, { 'Content-Type': 'application/json' });
            console.log(dir_data);
            res.end(JSON.stringify({
                'error_code': 0, 'error_msg': 'OK',
                'dir_data': dir_data, 'dir_path': new_path
            }));
        }
    }
});
srv.listen(8080, function () {
    console.log('listening on localhost:8080');
});



function readLocalFile(url) {
    let fs = require('fs');
    let result = { 'dir_array': [], 'file_array': [] };
    // console.log(url.length, url.split(''));
    try {
        let data = fs.readdirSync(url);
        console.log('dir_data', data);
        for (let i = 0; i < data.length; i++) {
            if (fs.statSync(path.join(url, data[i])).isDirectory() == true&&data[i]!='Source') {
                result.dir_array.push(data[i]);
            } else if(data[i].endsWith('gif')){
                result.file_array.push(data[i]);
            }
        }
        return result;
    } catch (error) {
        console.log('readLocalFile error!');
        return false;
    }
}


